package com.ywl.task.service.impl;

import com.ywl.task.mapper.MenuMapper;
import com.ywl.task.pojo.Menu;
import com.ywl.task.service.MenuService;
import com.ywl.task.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 严伟伦
 * @Company 无
 * @Description
 * @Date: 2024/9/5 16:30
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public QdResult queryMenus() {

        List<Menu> menus = queryMenuByParentId(0L);
        return QdResult.success(menus);
    }

    private List<Menu> queryMenuByParentId(long parentId) {
        //传0是找父菜单保存到menus
        List<Menu> menus = menuMapper.selectByParentId(parentId);

        //遍历这个是否有子菜单
        for (Menu menu: menus){
            if (menu.getMenuParent().equals(new Integer(2))){
                //有子菜单
                //递归自己查询，父菜单是自己的子菜单，放入subMenus
                List<Menu> subMenus = queryMenuByParentId(menu.getMenuId());

                //将子菜单放入children
                menu.setChildren(subMenus);

            }
        }

        return menus;
    }
}
